--普通攻击 attack
--翻滚 roll
--自动普通攻击 tap
--技能攻击 01skill 02skill 03skill 04skill 05skill
local SkillButton = require("app.battleV3.ui.objs.SkillButton")
local Attack = require("app.battleV3.ui.objs.Attack")
local RollButton = require("app.battleV3.ui.objs.RollButton")
local SkillLegendButton = require("app.battleV3.ui.objs.SkillLegendButton")
local TOP_ZORDER = 999

local SkillPanelView = MClass("SkillPanelView")

function SkillPanelView:ctor()
	--self.root 默认为根节点
	--self.root:setVisible(false)
	self.attack = Attack.new(self:findChild("00attack"))
	self.tab = self:findChild("btn_tap")
	self.roll = RollButton.new(self:findChild("btn_roll"))
	self.legend = SkillLegendButton.new(self:findChild("btn_artifact"))
	self.tips = self:findChild("btn_tap/tips")
	self.tipsLabel = self.tips:findChild("Panel/tlt")
	self.bg = self:findChild("bg")
	self.bg:setVisible(false)
	self.bg:setLocalZOrder(12)
	self.tab:setLocalZOrder(TOP_ZORDER)
	self.roll:setLocalZOrder(TOP_ZORDER)
	self.legend:setLocalZOrder(TOP_ZORDER)
	self.legend:setVisible(false)
	self.mSkillButtons = {}
	for i = 1, Constant.BattleSkillNum do
		self.mSkillButtons[i] = SkillButton.new(self:findChild(string.format("0%dskill",i)))
		self.mSkillButtons[i]:setVisible(false)
	end
	self:init()

    -- 新手战不显示
    if not app:isBattleGuideComp() then
        self.tab:setVisible(false)
		self.roll:setVisible(false)
	else
		self.tab:setVisible(true)
		self.roll:setVisible(true)
    end
end

function SkillPanelView:init()
	local function tabClickCallBack(sender, eventType)
		if eventType == ccui.TouchEventType.began then

		elseif eventType == ccui.TouchEventType.ended or eventType == ccui.TouchEventType.canceled then
			UD.setting:setKeepBasicAttack(not UD.setting:isKeepBasicAttack())
			local flagKeepBasicAttack = UD.setting:isKeepBasicAttack()
			self.tab:setBright(not flagKeepBasicAttack)
			BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.BATTLE_KEEPBKIL,{isKeep = flagKeepBasicAttack})
			if flagKeepBasicAttack then
				self.tips:setVisible(true)
				self.tipsLabel:setString(L("openautoattack"))
				self:performWithDelay(function()
					self.tips:setVisible(false)
				end,0.5)
			else
				self.tips:setVisible(true)
				self.tipsLabel:setString(L("closeautoattack"))
				self:performWithDelay(function()
					self.tips:setVisible(false)
				end,0.5)
			end
		end
	end
	self.tab:addTouchEventListener(tabClickCallBack)
	self.tab:setBright(not UD.setting:isKeepBasicAttack())
	BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.BATTLE_KEEPBKIL,{isKeep = UD.setting:isKeepBasicAttack()})

    -- 初始化UI
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.SKILL_INIT,handler(self,SkillPanelView.initSkills))
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.SKILL_UPDATE,handler(self,SkillPanelView.setButtonState))
    BattleUIEvent:addEventListener(BattleUIEvent.TYPE.SKILL_COMBO,handler(self,SkillPanelView.updateCombo))
	BattleUIEvent:addEventListener(BattleUIEvent.TYPE.SKILL_SHOW_REPLACE_BTN, handler(self,SkillPanelView.showReplaceButton))
	BattleUIEvent:addEventListener(BattleUIEvent.TYPE.SKILL_HIDE_REPLACE_BTN, handler(self,SkillPanelView.hideReplaceButton))

	BattleUIEvent:addEventListener(BattleUIEvent.TYPE.SKILL_SHOW_XULI_BTN, handler(self,SkillPanelView.showXuliButton))
	BattleUIEvent:addEventListener(BattleUIEvent.TYPE.SKILL_HIDE_XULI_BTN, handler(self,SkillPanelView.hideXuliButton))

	local function bgClickCallBack(sender, eventType)
		if eventType == ccui.TouchEventType.ended or eventType == ccui.TouchEventType.canceled then
			BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.SKILL_HIDE_REPLACE_BTN,self.mBranchRootSkillId)
			
		end
	end
	self.bg:setTouchEnabled(true)
	self.bg:addTouchEventListener(bgClickCallBack)
end

function SkillPanelView:updateCombo(data)
	local skillButton
	for i = 1, self.skillNum do
		skillButton = self.mSkillButtons[i]
		if skillButton and cc.isAlive(skillButton.root) then
			skillButton:lightStar(data)
		end
	end
end

function SkillPanelView:showXuliButton()
	self.bg:setVisible(true)
end

function SkillPanelView:hideXuliButton()
	self.bg:setVisible(false)
end

function SkillPanelView:showReplaceButton(skillId)
	self.bg:setVisible(true)
	self.mBranchRootSkillId = skillId
end

function SkillPanelView:hideReplaceButton(skillId)
	self.bg:setVisible(false)
	self.mBranchRootSkillId = nil
end

function SkillPanelView:initSkills(unit, classID, normalSkill, skills, rollSkill, legendSkill)
	for _, btn in pairs(self.mSkillButtons) do
		if btn and cc.isAlive(btn.root) then
			btn:setVisible(false)
		end
	end
	self.bg:setVisible(false)
	--设置普攻数据
	self.attack:setData(normalSkill, classID)

	--设置翻滚数据
	if rollSkill ~= nil then
		self.roll:setData(rollSkill)
	else
		self.roll:setVisible(false)
	end

	--设置传奇武器技能
	if legendSkill ~= nil then
		self.legend:setVisible(true)
		self.legend:setData(legendSkill)
	else
		self.legend:setVisible(false)
	end

	--设置技能属性
	self.skillNum = #skills
	print("======> self.skillNum:", self.skillNum)
	local buttonNum = #self.mSkillButtons
	local skillButton
	local skill
	for i = 1, buttonNum do
		skillButton = self.mSkillButtons[i]
		skill = skills[i]
		if skillButton and cc.isAlive(skillButton.root) then
			if skill then
				skillButton.skill = unit:getSkill(skill.skillCfg:getId())
				skillButton:setData(skills[i])
			end
		end
	end
end

-- 对外接口：初始化时候全显示、或者根据索引显示动画
function SkillPanelView:showSkillBtnName(name)
	if not name then
		local skillButton
		for i = 1, self.skillNum do
			skillButton = self.mSkillButtons[i]
			if skillButton and cc.isAlive(skillButton.root) then
				skillButton:setVisible(true)
			end
		end
	else
		self:_playSkillBtnShownAni(name)
	end
end

-- 技能图标出现动画
function SkillPanelView:_playSkillBtnShownAni(name)
	local skillButtonNode = self:findChild(name)
	if cc.isAlive(skillButtonNode) then
		skillButtonNode:setVisible(true)
	end
end

function SkillPanelView:setButtonState(alive, restrictedEffects, model, skills, currentGameTime, property,
                                       subscribeSkillId, showCanBreak)
	local skillBtns = self.mSkillButtons

	local skillAttack = skills[self.attack:getSkillId()]
	if skillAttack and skillAttack:inProgress() then
		BattleUIEvent:dispatchEvent(BattleUIEvent.TYPE.SKILL_ATTACK,true)
	end

	if self.roll:getSkillId() then
		local skillRoll = skills[self.roll:getSkillId()]
		if skillRoll then
			local isPending = model:isPending(self.roll:getSkillId())
			local preReleaseFinish = skillRoll:preReleaseFinish()
			local cd = skillRoll:cd()
			local cdD = skillRoll:cdD()
			local silenceTime = skillRoll:silenceTime()
			if silenceTime > cd then
				cd = silenceTime
			end

			local propertyCan = true
			local deployD = skillRoll:deployD()
			local isDeploy = skillRoll:deploying()
			local isReady = skillRoll:isReady()
			local deployTime = skillRoll:confirmTime() + deployD
			local deployLeftTime = deployTime - currentGameTime
			if deployLeftTime < 0 then
				deployLeftTime = 0
			elseif deployLeftTime > deployD then
				deployLeftTime = deployD
			end

			local priorityPass = true
			self.roll:setButtonState(alive, restrictedEffects, isPending, preReleaseFinish, cd, cdD,
				isDeploy, deployLeftTime, deployD, propertyCan, priorityPass, isReady, skillRoll, subscribeSkillId, showCanBreak)
		end
	end

	if self.legend:getSkillId() then
		local skillLegend = skills[self.legend:getSkillId()]
		if skillLegend then
			local isPending = model:isPending(self.legend:getSkillId())
			local preReleaseFinish = skillLegend:preReleaseFinish()
			local cd = skillLegend:cd()
			local cdD = skillLegend:cdD()
			local silenceTime = skillLegend:silenceTime()
			if silenceTime > cd then
				cd = silenceTime
			end

			local propertyCan = true
			local deployD = skillLegend:deployD()
			local isDeploy = skillLegend:deploying()
			local isReady = skillLegend:isReady()
			local deployTime = skillLegend:confirmTime() + deployD
			local deployLeftTime = deployTime - currentGameTime
			if deployLeftTime < 0 then
				deployLeftTime = 0
			elseif deployLeftTime > deployD then
				deployLeftTime = deployD
			end

			local priorityPass = true
			self.legend:setButtonState(alive, restrictedEffects, isPending, preReleaseFinish, cd, cdD,
				isDeploy, deployLeftTime, deployD, propertyCan, priorityPass, isReady, skillLegend, subscribeSkillId, showCanBreak)
		end
	end

	local skillButton
	for i = 1, self.skillNum do
		skillButton = skillBtns[i]
		if not skillButton or cc.isDead(skillButton.root) then
			return
		end
		local skillId = skillButton:getSkillId()
		local skill = skills[skillId]
		if skill then
			local isPending = model:isPending(skillId)
			local preReleaseFinish = skill:preReleaseFinish()
			local cd = skill:cd()
			local cdD = skill:cdD()
			local silenceTime = skill:silenceTime()
			if silenceTime > cd then
				cd = silenceTime
			end

			local mana = property.mana
			local manaCost = skill:manaCost()
			local rage = property.rage
			local rageCost = skill:rageCost()
			local energy = property.energy
			local energyCost = skill:energyCost()

			local propertyCan
			if mana and rage and energy then
				propertyCan = mana>=manaCost and rage>=rageCost and energy>=energyCost
			else
				return
			end

			local deployD = skill:deployD()
			local isDeploy = skill:deploying()
			local isReady = skill:isReady()
			local deployTime = skill:confirmTime() + deployD
			local deployLeftTime = deployTime - currentGameTime
			if deployLeftTime < 0 then
				deployLeftTime = 0
			elseif deployLeftTime > deployD then
				deployLeftTime = deployD
			end

			local priorityPass = true
			skillButton:setButtonState(alive, restrictedEffects, isPending, preReleaseFinish, cd, cdD, isDeploy, deployLeftTime, deployD, propertyCan, priorityPass, isReady, skill, subscribeSkillId, showCanBreak)
		end
	end
end

function SkillPanelView:enterAutoBattle()
	for k,btn in pairs(self.mSkillButtons) do
		if btn and cc.isAlive(btn.root) then
			btn:enterAutoBattle()
		end
	end
end

function SkillPanelView:exitAutoBattle()
	for k,btn in pairs(self.mSkillButtons) do
		if btn and cc.isAlive(btn.root) then
			btn:exitAutoBattle()
		end
	end
end

return SkillPanelView
